﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAlgorithms.Tests
{
    public class Experiments : ITestable
    {
        public void Test()
        {
            Exp01();
        }


        public void Exp01()
        {
            Console.Write("请输入矩阵的维度n:");
            var input = Console.ReadLine();
            int n = int.Parse(input + "");
            int[,] matrix = MakeMatrix(n);
            printMatrix(matrix);
        }



        public static int[,] MakeMatrix(int n)
        {
            // n in [1, 10)
            n = n < 1 ? 1 : (n > 10 ? 10 : n);
            int[,] matrix = new int[n, n];
            int i = 0, j = 0;//二维数组的两个下标
            int cout = 1;
            int loops = 0;     //当前圈数
            while (loops < n / 2)//依次为每一圈得元素赋值
            {
                for (i = loops, j = loops; i < n - loops - 1; i++)
                    matrix[i, j] = cout++;
                for (j = loops; j < n - loops - 1; j++)
                    matrix[i, j] = cout++;
                for (i = n - loops - 1; i > loops; i--)
                    matrix[i, j] = cout++;
                for (j = n - loops - 1; j > loops; j--)
                    matrix[i, j] = cout++;
                loops++;
            }
            if (n % 2 != 0)//n为奇数的情况，为中间元素赋值
                matrix[n / 2, n / 2] = n * n;

            for (i = 0; i < matrix.GetLength(0); i++)
            {
                for (j = 0; j < matrix.GetLength(0); j++)
                {
                    matrix[i, j] = n * n - matrix[i, j];
                }
            }


            return matrix;
        }

        static void printMatrix(int[,] matrix)
        {
            Console.WriteLine($"The {matrix.GetLength(0)}-dimention matrix is shown belown:");//输出方阵
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                for (int j = 0; j < matrix.GetLength(0); j++)
                {
                    if (matrix[i, j] < 10)
                        Console.Write("   {0}", matrix[i, j]);
                    else
                        Console.Write("  {0}", matrix[i, j]);
                }
                Console.Write("\n\n");
            }
        }

        enum Turn { Down, Right, Up, Left }

        /// <summary>
        /// 生成矩阵的另一种方法。
        /// </summary>
        static void MakeMatrix2()
        {
            Console.Write("输入矩形长度(正整数):");
            uint width;
            while (!uint.TryParse(Console.ReadLine(), out width))
            {
                Console.Write("输入矩形长度(正整数):");
            }
            int[,] intArr = new int[width, width];
            Turn turn = Turn.Down;
            intArr[0, 0] = 1;
            int i = 1, j = 0;
            int num = 1;
            int turnCount = 1;
            while (num < width * width)
            {
                switch (turn)
                {
                    case Turn.Down:
                        intArr[i, j] = ++num;
                        if (i == width - turnCount)
                        {
                            turn = (Turn)(((int)turn + 1) % 4);

                            j++;
                            break;
                        }
                        i++;
                        break;
                    case Turn.Right:
                        intArr[i, j] = ++num;
                        if (j == width - turnCount)
                        {
                            turn = (Turn)(((int)turn + 1) % 4);
                            i--;
                            break;
                        }
                        j++;
                        break;
                    case Turn.Up:
                        intArr[i, j] = ++num;
                        if (i == turnCount - 1)
                        {
                            turn = (Turn)(((int)turn + 1) % 4);
                            j--;
                            break;
                        }
                        i--;
                        break;
                    case Turn.Left:
                        intArr[i, j] = ++num;
                        if (j == turnCount)
                        {
                            turn = (Turn)(((int)turn + 1) % 4);

                            turnCount++;
                            i++;
                            break;
                        }
                        j--;
                        break;
                }
            }
            for (int k = 0; k < intArr.GetLength(0); k++)
            {
                for (int z = 0; z < intArr.GetLength(1); z++)
                {
                    Console.Write("{0,-4}", intArr[k, z]);
                }
                Console.WriteLine("\n");
            }
        }
    }
}
